package com.itmk.web.sys_menu.entity;

import com.itmk.web.sys_role.entity.SysRole;
import org.springframework.beans.BeanUtils;

import java.security.PublicKey;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

public class MakeMenuTree {

    public static List<SysMenu> makeTree(List<SysMenu> menuList,Long pid) {
        List<SysMenu> menus = new ArrayList<>();
        //组装树形结构
        Optional.ofNullable(menuList).orElse(new ArrayList<>())
                .stream()
                .filter(item->item!=null&&item.getParentId().equals(pid))
                .forEach(item->{
                    SysMenu menu = new SysMenu();
                    BeanUtils.copyProperties(item,menu);
                    menu.setLabel(item.getTitle());
                    menu.setValue(item.getMenuId());
                    //查找下级，递归调用
                    List<SysMenu> children = makeTree(menuList, item.getMenuId());
                    menu.setChildren(children);
                    menus.add(menu);
                });
        return menus;
    }
    public static List<RouterVo> makeRouter(List<SysMenu> menuList ,Long pid){
        List<RouterVo> list = new ArrayList<>();
        Optional.ofNullable(menuList).orElse(new ArrayList<>())
                .stream()
                .filter(item->item!=null && item.getParentId().equals(pid))
                .forEach(item->{
                    RouterVo router = new RouterVo();
                    router.setName(item.getName());
                    router.setPath(item.getPath());
                    //设置children属性需要递归调用
                    List<RouterVo> children = makeRouter(menuList,item.getMenuId());
                    router.setChildren(children);
                    //判断是哪种类型
                    if(item.getParentId()==0L){
                        //如果上级是0，那layout就是component,也就是首页，其实可以交给前端设计
                        router.setComponent("layout");
                        //判断数据是目录还是菜单,如果是一级菜单，是菜单类型，单独处理
                        if(item.getType().equals("1")){
                            router.setRedirect(item.getPath());
                            List<RouterVo> listChild = new ArrayList<>();
                            RouterVo child = new RouterVo();
                            child.setName(item.getName());
                            child.setPath(item.getPath());
                            child.setComponent(item.getUrl());
                            child.setMeta(child.new Meta(
                                    item.getTitle(),
                                    item.getIcon(),
                                    item.getCode().split(",")
                            ));
                            listChild.add(child);
                            router.setChildren(listChild);
                            router.setPath(item.getPath()+"parent");
                            router.setName(item.getName()+"parent");
                        }

                    }else{
                        router.setComponent(item.getUrl());
                    }
                    router.setMeta(router.new Meta(
                            item.getTitle(),
                            item.getIcon(),
                            item.getCode().split(",")
                    ));
                    list.add(router);
                });
        return list;
    }

}
